<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/18
 * Time: 10:55
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;
use think\facade\View;
use think\facade\Filesystem;

class groups extends AdminBase
{
    public function index()
    {
        $agency = \app\common\model\Agency::getAgency();
        return View::fetch('index',['agency'=>$agency]);
    }

    //分类管理
    public function groups_json()
    {
        $map = [];
        $groups = input('groups');
        $bill = input('bill');
        $policy = input('policy');

        if(!empty($groups)){
            array_push($map,['a.name','like','%'.$groups.'%']);
        }

        if(!empty($bill)){
            array_push($map,['c.name','like','%'.$bill.'%']);
        }

        if(!empty($policy)){
            array_push($map,['b.name','like','%'.$policy.'%']);
        }

        $list = Db::name('groups')
            ->alias('a')
            ->join('policy b','a.pid = b.id')
            ->join('bill c','c.id = b.bid')
            ->where($map)
            ->order('a.create_time')
            ->field('a.*,b.car,b.displacement,b.name as pname,b.city,c.name as bname,b.id as pid')
            ->paginate(10)
            ->each(function($item, $key){
            switch($item['car']){
                case '0':
                    $item['car'] = '新车';break;
                case '1':
                    $item['car'] = '旧车';break;
                default:
                    $item['car'] = '全部';break;
            }
            switch($item['displacement']){
                case '0':
                    $item['displacement'] = '<250';break;
                case '1':
                    $item['displacement'] = '>=250';break;
                default:
                    $item['displacement'] = '全部';break;
            }
                $item['commercial_type'] = $item['commercial_type'] == 0 ? '保险金额比例' : '每单固定金额';
                $item['compolsury_type'] = $item['compolsury_type'] == 0 ? '保险金额比例' : '每单固定金额';
            return $item;
        });

        $this->result($list);
    }

    //分类添加
    public function groups_add()
    {
        if ($this->request->isPost()) {
            $param = input('post.');

            $validate = new \app\admin\validate\Groups();
            $result = $validate->check($param);

            if(!$result){
                $this->error($validate->getError());
            }

            if($param['isedit'] == 0){
                $param['create_time'] = time();
            }

            if(!Db::name('policy')->where('id',$param['pid'])->value('id')){
                $this->error('政策不存在，请确认政策编号是否正确');
            }
            $param['update_time'] =time();
            $param['uid'] = session('admin_auth.admin_id');
            if($param['isedit'] == 1){
                $book = \app\common\model\Groups::find($param['id']);
            }else{
                $book = new \app\common\model\Groups();
            }

            $msg = $book->allowfield([])->save($param);
            if($msg){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }

        $aid = input('aid');
        $aname = Db::name('agency')->where('id',$aid)->value('name');
        $cid = input('cid');
        $cname = Db::name('company')->where('id',$cid)->value('name');
        $agency = \app\common\model\Agency::getAgency();
        $company = \app\common\model\Company::getCompany($aid);
        $bill = \app\common\model\Bill::getBill($aid,$cid);
        $isedit = input('isedit');
        $map =['isedit'=> $isedit,'agency'=>$agency,'company'=>$company,'bill'=>$bill,'aid'=>$aid,'aname'=>$aname,'cid'=>$cid,'cname'=>$cname,'type'=>input('type',0)];

        if($isedit == 1){
            $data = Db::name('groups')->where('id',input('id'))->find();
            $map['data'] = $data;
        }

        return $this->fetch('groups_edit',$map);
    }

    //分类删除
    public function groups_del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();

            if(session('admin_auth.admin_id') != 1) $this->error('无权进行此操作');

            Db::name('groups')->where('id','in',$param['id'])->delete();
            insert_admin_log('删除了保险公司');
            $this->success('删除成功');
        }
    }

    //分类删除
    public function groups_employ_del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();

            if(session('admin_auth.admin_id') != 1) $this->error('无权进行此操作');

            Db::name('employee')->where('id','in',$param['id'])->update(['gid'=>'']);
            $this->success('删除成功');
        }
    }

    public function editEmployee(){
        $id = input('id');
        $agency = \app\common\model\Agency::getAgency();
        $count = Db::name('employee')->where('gid',$id)->count();
        return View::fetch('editEmployee',['agency'=>$agency,'ecount'=>$count,'gid'=>$id]);
    }

    public function employee_json(){
        $map = [['a.gid','=',input('gid')]];
        $start_time = input('start_time','1970-10-01');
        $end_time = input('end_time',date('Y-m-d',time()));
        $ccode = input('ccode');
        $code = input('code');
        $aid = input('aid');


        if(input('start_time') != ''){
            $start_time = strtotime($start_time);
        }else{
            $start_time = strtotime('1970-10-01');
        }

        if(input('end_time') != ''){
            $end_time = strtotime($end_time);
        }else{
            $end_time = time();
        }
        $map = [['a.create_time','between',[$start_time,$end_time]]];

        if(!empty($aid)){
            array_push($map,['a.aid','=',$aid]);
        }

        if(!empty( $ccode)){
            array_push($map,['b.code','like','%'.$ccode.'%']);
        }

        if(!empty($code)){
            array_push($map,['a.code','like','%'.$code.'%']);
        }



        $list = Db::name('employee')
            ->alias('a')
            ->join('channel b','a.channelid = b.id')
            ->where($map)
            ->whereBetweenTime('a.create_time', $start_time, $end_time)
            ->order('a.create_time')
            ->field('a.*,b.name as cname,b.code as ccode')->paginate(10)->each(function($item, $key){
                $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
                return $item;
            });

        $this->result($list);
    }

    public function getCompany(){
        if ($this->request->isPost()) {
            $param = $this->request->param();

            $data = Db::name('company')->where('aid',$param['id'])->field('name,id')->select();

            return $data;
        }
    }

    public function getBill(){
        if ($this->request->isPost()) {
            $param = $this->request->param();

            $data = Db::name('bill')->where('cid',$param['id'])->field('name,id')->select();

            return $data;
        }
    }
}